<?php
namespace app\admin\controller\base;
use app\admin\model\Admin;
use app\common\controller\Backend;
use fast\Tree;
use app\common\model\Config as ConfigModel;


class Config extends Backend
{
    
    /**
     * Sale模型对象
     * @var \app\admin\model\Sale
     */
    protected $model = null;
    protected $show_model = 'plan,price';
    protected $show_user = 'plan_copy_person,plan_Auditor,SendLecturers_Leader,SendLecturers_SomeoneElse';

    public function _initialize()
    {
        parent::_initialize();
        $this->loadlang('general.config','admin'); 
        $this->model = model('Config');
        $this->view->assign('show_user', $this->show_user);
    }
    
    
    /**
     * 配置发送信息给某个 人 部门 标签，
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {
        
        $siteList = [];
        $groupList = ConfigModel::getGroupList();
        foreach ($groupList as $key => $value) {
             if(!ValueExists($key,$this->show_model)){
                unset($groupList[$key]);
             }else{
                $siteList[$key]['name'] = $key;
                $siteList[$key]['title'] = $value;
                $siteList[$key]['list'] = [];
             }
        }
       
        foreach ($this->model->all() as $k => $v) {

            if (!isset($siteList[$v['group']])) {
                continue;
            }
            $value = $v->toArray();
            $value['title'] = __($value['title']);
            if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                $value['value'] = explode(',', $value['value']);
            }
            $value['content'] = json_decode($value['content'], TRUE);
            $siteList[$v['group']]['list'][] = $value;
        }
        $index = 0;
        foreach ($siteList as $k => &$v) {
            $v['active'] = !$index ? true : false;
            $index++;
        }
        $this->view->assign('siteList', $siteList);
        $this->view->assign('typeList', ConfigModel::getTypeList());
        $this->view->assign('groupList', $groupList);
        return $this->view->fetch();
    }
    /**
     * 编辑
     * @param null $ids
     */
    public function edit($ids = NULL)
    {
        if ($this->request->isPost()) {
            $row = $this->request->post("row/a");
            if ($row) {
                $configList = [];
                foreach ($this->model->all() as $v) {
                    if (isset($row[$v['name']])) {
                       if(!ValueExists($v['name'],$this->show_user)){
                         $value = $row[$v['name']];
                      }else{
                        $value = implode('|',explode(',',$row[$v['name']]));
                      }
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }
                      $v['value'] = $value;
                      $configList[] = $v->toArray();
                    }
                }
                $this->model->allowField(true)->saveAll($configList);
                try {
                    $this->refreshFile();
                    $this->success();
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }
    /**
     * 刷新配置文件
     */
    protected function refreshFile()
    {
        $config = [];
        foreach ($this->model->all() as $k => $v) {

            $value = $v->toArray();
            if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
                $value['value'] = explode(',', $value['value']);
            }
            if ($value['type'] == 'array') {
                $value['value'] = (array)json_decode($value['value'], TRUE);
            }
            $config[$value['name']] = $value['value'];
        }
        file_put_contents(APP_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export($config, true) . ";");
    }


}
